using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

namespace WebAndLoadTestPlugins
{
    /// <summary>
    /// Extracts an query string parameter
    /// </summary>
    public class ExtractQueryStringParam : ExtractionRule
    {
        string m_anchorId;
        string m_queryStringParameterName;
        public string AnchorId
        {
            get { return m_anchorId; }
            set { m_anchorId = value; }
        }

        public string QueryStringParameterName
        {
            get { return m_queryStringParameterName; }
            set { m_queryStringParameterName = value; }
        }

        public override void Extract(object sender, ExtractionEventArgs e)
        {
            // Loop over the HTML tags
            foreach (HtmlTag htmlTag in e.Response.HtmlDocument.HtmlTags)
            {
                // Find the A tag
                if(String.Equals("a", htmlTag.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    // Find the link
                    if(String.Equals(m_anchorId, htmlTag.GetAttributeValueAsString("id")))
                    {
                        string href = htmlTag.GetAttributeValueAsString("href");
                        int indexOfParam = href.IndexOf(m_queryStringParameterName);
                        if (indexOfParam == -1)
                        {
                            e.Success = false;
                            e.Message = String.Format("Could not find Query String Param '{0}'.", m_queryStringParameterName);
                            return;
                        }
                        int indexOfValue = indexOfParam++;
                        int indexOfEndOfValue = href.IndexOf('&');
                        int length;
                        if (indexOfEndOfValue == -1)
                        {
                            length = href.Length - indexOfValue;
                        }
                        else
                        {
                            length = indexOfEndOfValue - indexOfValue;
                        }

                        string value = href.Substring(indexOfValue, length);
                        
                        // Add the extracted product ID to the WebTest's context
                        e.WebTest.Context[this.ContextParameterName] = value;
                        
                        // Set the success flag and a message that will be shown in playback
                        e.Success = true;
                        e.Message = String.Format("Extracted value '{0}'.", value);
                        return;
                    }
                }
            }

            e.Success = false;
            e.Message = "Could not find the product ID.";
        }
        public override string RuleName
        {
            get { return "Query String Param"; }
        }
    }
}
